package ui;

import Pojo.Account;
import Service.AccountService;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.List;

public class LoginFrame extends JFrame implements ActionListener {
    private JTextField txName ;
    private JPasswordField txPwd ;

    public LoginFrame(){
        setTitle("图书管理系统"); //设置窗口的标题
        setResizable(false); //是否可改变大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭按钮的时候如何处理
        setBounds(100,100,515,444); //设置窗口的大小和展示位置
        JPanel conenetPane = new JPanel(); //创建一个当前窗口的面板
//设置面板的边框
        conenetPane.setBorder(new EmptyBorder(5,5,5,5));
        setContentPane(conenetPane);//设置当前页面需要展示的主面板
        conenetPane.setLayout(null); //设置面板的布局方式 null
        JLabel labelNewLabel = new JLabel("new label"); //容器组件 可以额包含任意内容
//获取当前图片的的地址
        URL url = LoginFrame.class.getResource("../image/ashi.jpg");
        System.out.println(url.toString());
        Icon icon = new ImageIcon(url);//创建一个Icon对象，该对象必须依赖图片的地址创建
        labelNewLabel.setIcon(icon); //存放图片图表
        labelNewLabel.setBounds(5,0,509,235);
        conenetPane.add(labelNewLabel);//把图片标签容器添加到面板中
//设置账户名称和密码提示
        JLabel nameTitle = new JLabel("账户名");
        nameTitle.setFont(new Font("楷体",Font.PLAIN,18));
        nameTitle.setBounds(69,266,54,15);
        conenetPane.add(nameTitle);
        JLabel passwordTitle = new JLabel("密码");
        passwordTitle.setFont(new Font("楷体",Font.PLAIN,18));
        passwordTitle.setBounds(69,313,54,15);
        conenetPane.add(passwordTitle);
        txName = new JTextField();
        txName.setFont(new Font("楷体",Font.PLAIN,18));
        txName.setBounds(133,257,237,32);
        txName.setColumns(10);
        conenetPane.add(txName);
//设置密码框
        txPwd = new JPasswordField();
        txPwd.setEchoChar('0');
        txPwd.setFont(new Font("楷体",Font.PLAIN,18));
        txPwd.setBounds(133,304,237,32);
        txPwd.setColumns(10);
        conenetPane.add(txPwd);
//设置登录和注册按钮
        JButton btnLogin = new JButton("登录");
        btnLogin.setFont(new Font("楷体",Font.PLAIN,18));
        btnLogin.setBounds(97,357,104,31);
        btnLogin.addActionListener(this); //给按钮 添加事件监听
        conenetPane.add(btnLogin);
//注册
        JButton btnExit = new JButton("退出");
        btnExit.setFont(new Font("楷体",Font.PLAIN,18));
        btnExit.setBounds(298,357,104,31);
        btnExit.addActionListener(this);
        conenetPane.add(btnExit);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
        if(e.getActionCommand().equals("退出")){
            System.exit(0);// 正常终止程序
        } else if (e.getActionCommand().equals("登录")) {

            String name = txName.getText();
            String pwd = new String(txPwd.getPassword());

            AccountService accountService = new AccountService();

            List<Account> list = accountService.lodingAccountAll();
            for (Account account : list) {
                if (account.getUsername().equals(name) && account.getPassword().equals(pwd)) {
                    MainFrame mainFrame = new MainFrame(name);
                    this.dispose();
                    return;
                }
            }

            JOptionPane.showMessageDialog(this,"对不起，账户密码错误");
        }
    }
}
